package com.qingneng.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.qingneng.service.sdk.IContractInfoService;
import com.qingneng.service.sdk.IProjectInfoService;
import com.qingneng.vo.ProjectInfo;
import com.tbfs.common.BaseAction;
import com.tbfs.security.CurrentUser;

public class SaveProjectInfoAction extends BaseAction implements ModelDriven<ProjectInfo>{
	
	private static final Logger logger = Logger.getLogger(SaveProjectInfoAction.class);
	
	// init service
	private IProjectInfoService projectInfoService;
	
	private IContractInfoService contractInfoService;
	private ProjectInfo projectInfo = new ProjectInfo();
	
	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}

	// 构造函数
	public SaveProjectInfoAction(IProjectInfoService projectInfoService) {
		this.projectInfoService = projectInfoService;
	}

	public SaveProjectInfoAction() {

	}
	
    private String message = null;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String execute(){
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin save projectInfo execute +++");
		}
		try {
			logger.info("contract_id:"+projectInfo.getContractinfo_id());
			//判断是新增，还是修改
			logger.info("projectInfo.id:"+projectInfo.getId());
			//表示新增
			if(projectInfo.getId()==0){
				projectInfo.setProjectStatus(0);
				projectInfo.setCreatePerson(CurrentUser.getUser().getUseraccount());
				int new_id =  projectInfoService.addProjectInfo(projectInfo);
				//1表示增加成功
				projectInfo.setMessage("1");
				//return seq's value key 
				projectInfo.setId(new_id);
				
			}
			//表示修改
			else{
				//2表示修改成功
				projectInfoService.modifyProjectInfo(projectInfo);
				projectInfo.setMessage("2");
			}
					
			
		} catch (Exception e) {
			projectInfo.setMessage("failure");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++ end save projectInfo execute +++");
		}
		return Action.SUCCESS;
	}	
	
	public String destroy(){
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin destroy projectInfo execute +++");
		}
		try {
			logger.info("projectInfo_id:"+projectInfo.getId());
			projectInfoService.destroyProjectInfo(projectInfo);
			message = "success";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++ end destroy projectInfo execute +++");
		}
		return Action.SUCCESS;
	}
	

	public ProjectInfo getModel() {
		// TODO Auto-generated method stub
		return projectInfo;
	}
	
	/**
	 * 审核
	 * @return
	 */
    public String audit(){	
    	try {
			projectInfoService.modifyProjectInfo(projectInfo);
			projectInfo.setMessage("true");
		} catch (Exception e) {
			projectInfo.setMessage("false");
			e.printStackTrace();
		}	
    	return Action.SUCCESS;
    }

	public String saveProjectInfoForBatch(){
		HttpServletRequest request = getRequest();
		String projectInfoList = request.getParameter("projectInfoList");
		logger.info("projectInfoList--------="+projectInfoList);
		Gson json = new Gson();
		List<ProjectInfo> list = json.fromJson(projectInfoList, new TypeToken<List<ProjectInfo>>() {
		}.getType());
		int  contractPrice = 0;
		int contractId = 0;
		for (ProjectInfo projectInfo : list) {
			contractPrice = contractPrice + Integer.parseInt(projectInfo.getContractPrice());
			contractId = projectInfo.getContractinfo_id();
			try {
				projectInfoService.modifyProjectInfo(projectInfo);
				projectInfo.setMessage("true");
			} catch (Exception e) {
				projectInfo.setMessage("false");
				e.printStackTrace();
			}
		}
		
		return Action.SUCCESS;
	}
	

}
